// JavaScript Document
$(function () {

    var message = {};
    message.init = function () {}
    // swiper轮播插件
    var swiper = new Swiper('.mod-swiper', {
        pagination: '.mod-swiper .swiper-pagination',
        paginationClickable: true,
        autoplay: 3000,
        autoplayDisableOnInteraction: false,
        loop: true
    });

    /*** 验证用户是否登录并获取信息*/
    //validateIsLogin();

    $(".j-radio").each(function () {
        $(this).jRadio(); //单选按钮
    });

    $(".j-select").each(function () {
        $(this).jSelect(); //单选按钮
    });

    $(".j-checkbox").each(function () {
        $(this).jCheckBox(); //复选按钮
    });

    $('.li-other').on('click', function () {
        if ($('.other-text').is(":hidden")) {
            $('.other-text').show();
        } else {
            $('.other-text').hide();
        }
    })

    demandDesc(); // 需求描述选择

    window.onload = function () {
        $('.demandDescription').on('keyup', function () {
            var oTxt = $(this);
            var oTxtVal = oTxt.val();
            if (oTxtVal.length >= 1000) {
                alertTip("需求描述不能超过1000个字！", 2000);
                oTxt.val(oTxtVal.substr(0, 1000));
            }
        });
    }
    //免费评估
    $('.j-freeEvaluating').on('click', function () {
        sessionStorage.removeItem('StepTips');
        sessionStorage.removeItem('id');
        sessionStorage.removeItem('project_form');
        sessionStorage.removeItem('demandModuleItems');
        sessionStorage.removeItem('demandModuleItemsFirst');
        sessionStorage.removeItem('demandModuleItemsSecond');
        sessionStorage.removeItem('five_to_one');
        sessionStorage.removeItem('save');
        sessionStorage.removeItem('target_url');
        sessionStorage.removeItem('respRoles');
        sessionStorage.removeItem('message_roles_url');
        sessionStorage.removeItem('project_model_url');
        sessionStorage.removeItem('saveSuccess');
        sessionStorage.removeItem('page_tab_2');
        sessionStorage.removeItem('page_tab_3');
        sessionStorage.removeItem('page_tab_4');

        window.location.href = "message_project.html";
    });

    //浮框关闭
    $(".float-close").on("click", function () {
        $(".float-frame").slideUp('slow');
        sessionStorage.StepTips = 0;
    });

    // 手机验证
    $("#btn-msg-verify").on("click", function () {
        var tel = $.trim($("#contactTel").val());
        var captcha = $('#captchaCode').val();
        var testValue = /^[1][3-8][0-9]{9}$/i.test(tel);

        if (!tel) {
            alertTip("请输入手机号！", 2000);
            return false;
        }
        if (!testValue) {
            alertTip("手机号格式不正确！", 2000);
            $('#btn-msg-verify').removeClass('disabled');
            return false;
        }

    });

    //提交表单
    $("#submit_forming").on("click", function () {
        saveApply();
    });


    // 需求描述选择
    function demandDesc() {
        $('.demand-type a').on('click', function () {
            if (!$(this).hasClass('current')) {
                var descType = $(this).attr('data-type');
                $(this).addClass('current').siblings('a').removeClass('current');

                $('.damenddesc-box').find('.ui-textarea[data-type="' + descType + '"]').removeClass('hide').siblings('.ui-textarea').addClass('hide');
            } else {
                var descType = 0;
                $(this).removeClass('current');
                $('.damenddesc-box').find('.ui-textarea[data-type="' + descType + '"]').removeClass('hide').siblings('.ui-textarea').addClass('hide');
            }
        });
    }
    //传参数
    function saveApply() {
        //描述
        var demandDescription;
        $('.demandDescription').each(function () {
            if (!$(this).hasClass('hide')) {
                demandDescription = $.trim($(this).val());
            }
        });
        if (demandDescription.length == 0) {
            alertTip("请填写需求描述！", 2000);
            return;
        }
        if (demandDescription.length > 1000) {
            alertTip("需求描述不能超过1000个字！", 2000);
            return;
        }
        //预算
        if ($('.budget-1').is(":hidden")) {
            var budgetRange = $.trim($('.budget-2 li.checked').text());
        } else {
            var budgetRange = $.trim($('.budget-1 li.checked').text());
        }
        if (budgetRange == '') {
            alertTip("请选择预算！", 2000);
            return false;
        }
        //名字
        var contactName = $.trim($("#contactName").val());
        if (contactName.length <= 0) {
            alertTip("请填写您的称呼！", 2000);
            return;
        }
        if (contactName.length > 50) {
            alertTip("您的称呼不能超过50个字！", 2000);
            return;
        }
        //手机
        var contactTel = $.trim($("#contactTel").val());
        if (contactTel.length <= 0) {
            alertTip("请输入手机号！", 2000);
            return;
        }
        if (!isMobile(contactTel)) {
            alertTip("手机号格式不正确！", 2000);
            return;
        }

        //取cookies函数
        function getCookie(name) {
            var arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
            if (arr != null) return unescape(arr[2]);
            return null;
        }
        var channel = getCookie("UWORKS_CHANNEL");
        if (channel == null || channel == "") {
            channel = window.localStorage.getItem("UWORKS_CHANNEL");
            if (channel == null) {
                channel = "";
            }
        }

        // console.log({
        //         "demandDescription": demandDescription,
        //         "budgetRange": budgetRange,
        //         "contactName": contactName,
        //         "contactTel": contactTel,
        //         "code": code,
        //         "channel":channel
        //     });
        // return false;

        //构建邮件内容
        var content = "来自联拓官网邮件<br>";
        content += "客户姓名：" + contactName;
        content += " 电话号码：" + contactTel + "<br>";
        content += "需求描述：" + demandDescription + "<br>";
        content += "预算：" + budgetRange + "<br>";

        var ajaxData = {
            Body: content,
            Address: "", //留空从服务器取地址
            Subject: "联系我们"
        };
        console.log(ajaxData);
        $.ajax({
            url: "/mail/Send",
            type: "POST",
            dataType: "json",
            data: ajaxData,
            // beforeSend: function (xhr) {
            //     xhr.setRequestHeader("Content-Type", "application/json");
            // },
            // data: JSON.stringify({
            //     "demandDescription": demandDescription,
            //     "budgetRange": budgetRange,
            //     "contactName": contactName,
            //     "contactTel": contactTel,
            //     "channel":channel
            // }),
            success: function (data) {
                var respResultId = data;
                $("#input_hide").attr("data-respResultId", respResultId);
                // window.location.href = "message_success_jump.html?respResultId=" + respResultId;
                $(".tcc-success,.cover").show();
                var window_top = $(window).scrollTop();
                var windowW = $(window).width();
                if (windowW < 1220 && windowW >= 1) {
                    $('.tcc-success').css({
                        'top': window_top + 50
                    });
                }
                $('.close-btn').on('click', function () {
                    $(".tcc-success,.cover").hide();
                    window.location.reload();
                });

            },
            error: function (resp) {
                //alertTip(JSON.parse(resp.responseText).message);
                alertTip("网络故障,请稍后重试。");
            }
        });
    }

    message.init();
});